// 积分商品
// import { isEmpty } from 'lodash'
import * as types from '@/store/mutation-types'
import api from '@/tools/api.js'
import promise from '@/tools/promise'
// import { SUCCESS_STATUS } from '@/config/status'
const state = {
  list: [],
  fileList: [],
  pageData: {
    total: 1,
    per_page: 15,
    current_page: 1,
    last_page: 1
  },
  saveData: {},
  pic: {}
}

// mutations
const mutations = {
  [types.INTEGRAL_DOODS_LIST] (state, payload) {
    state.list = payload.data
    state.pageData.total = Number(payload.total)
    state.pageData.per_page = Number(payload.per_page)
    state.pageData.current_page = Number(payload.current_page)
    state.pageData.last_page = Number(payload.last_page)
  },
  [types.INTEGRAL_SAVE_DATA] (state, payload) {
    state.saveData = payload
  },
  [types.INTEGRAL_EDIT_SAVE_DATA] (state, res) {
    state.saveData = res
    let fileIds = res.imgs_ids
    let fileUrls = res.imgs_urls
    let fileList = []
    fileIds = fileIds.split(',')
    fileUrls = fileUrls.split(',')
    for (let i = 0; i < Number(fileIds.length); i++) {
      fileList.push({name: fileIds[i], url: fileUrls[i]})
    }
    state.fileList = fileList
  },
  [types.MALLPICDATA] (state, res) {
    state.pic = res
  }
}

// actions
const actions = {
  // 获取积分商品列表
  async allGoodsList ({ commit }, payload) {
    let config = {loading: true, loadingText: '正在获取积分商品列表'}
    let res = await api('serverintegralmall/goods/Goods/allGoodsList', payload, config)
    commit(types.INTEGRAL_DOODS_LIST, res.res)
  },
  // 积分商品下架
  async soldOutOneGoods ({ commit }, payload) {
    let config = {loading: true, loadingText: '正在下架商品'}
    let res = await api('serverintegralmall/goods/Goods/soldOutOneGoods', payload, config)
    return promise(res)
  },
  // 积分商品删除
  async manageDelGoods ({ commit }, payload) {
    let config = {loading: true, loadingText: '正在删除商品'}
    let res = await api('serverintegralmall/goods/Goods/manageDelGoods', payload, config)
    return promise(res)
  },
  // 积分商品添加
  async addIntegralGoods ({ commit }, payload) {
    let config = {loading: true, loadingText: '正在添加商品'}
    let res = await api('serverintegralmall/goods/Goods/addIntegralGoods', payload, config)
    return promise(res)
  },
  // 积分商品编辑
  async editIntegralGoods ({ commit }, payload) {
    let config = {loading: true, loadingText: '正在编辑商品'}
    let res = await api('serverintegralmall/goods/Goods/updateIntegralGoods', payload, config)
    return promise(res)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
